package cn.com.idmy.id.lifecycle;


/**
 * 抽象生命周期，状态不能重复
 */
public abstract class AbstractIdGeneratorLifecycle implements IdGeneratorLifecycle {
    public static final String MSG_FORMAT = "前一个状态必须为: %s,当前为: %s";
    protected State state = State.DEFAULT_STATE;

    @Override
    public State getState() {
        return state;
    }

    @Override
    public void init() {
        if (getState() != State.DEFAULT_STATE) {
            throw new IllegalArgumentException(String.format(MSG_FORMAT, State.DEFAULT_STATE.value(), getState().value()));
        } else {
            doInit();
            state = State.INIT_STATE;
        }
    }

    @Override
    public void start() {
        if (getState() != State.INIT_STATE) {
            throw new IllegalArgumentException(String.format(MSG_FORMAT, State.INIT_STATE.value(), getState().value()));
        } else {
            doStart();
            state = State.START_STATE;
        }
    }

    @Override
    public void stop() {
        if (getState() != State.START_STATE) {
            throw new IllegalArgumentException(String.format(MSG_FORMAT, State.START_STATE.value(), getState().value()));
        } else {
            doStop();
            state = State.STOP_STATE;
        }
    }

    public void doInit() {
    }

    public void doStart() {
    }

    public void doStop() {
    }
}
